home *** CD-ROM | disk | FTP | other *** search
- Imports System.Drawing.Drawing2D
-
- Public Class DrawingPrimitives
- Inherits System.Windows.Forms.Form
-
- #Region " Windows Form Designer generated code "
-
- Public Sub New()
- MyBase.New()
-
- 'This call is required by the Windows Form Designer.
- InitializeComponent()
-
- 'Add any initialization after the InitializeComponent() call
-
- End Sub
-
- 'Form overrides dispose to clean up the component list.
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
- Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
- Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
- Friend WithEvents mnuPaint As System.Windows.Forms.MenuItem
- Friend WithEvents mnuResize As System.Windows.Forms.MenuItem
- Friend WithEvents mnuLines As System.Windows.Forms.MenuItem
- Friend WithEvents MenuItem9 As System.Windows.Forms.MenuItem
- Friend WithEvents mnuPolygons As System.Windows.Forms.MenuItem
- Friend WithEvents mnuCardinal As System.Windows.Forms.MenuItem
- Friend WithEvents mnuBezier As System.Windows.Forms.MenuItem
- Friend WithEvents mnuPens As System.Windows.Forms.MenuItem
- Friend WithEvents mnuPenAlignment As System.Windows.Forms.MenuItem
- Friend WithEvents mnuDashedLines As System.Windows.Forms.MenuItem
- Friend WithEvents mnuPaths As System.Windows.Forms.MenuItem
-
- 'Required by the Windows Form Designer
- Private components As System.ComponentModel.Container
-
- 'NOTE: The following procedure is required by the Windows Form Designer
- 'It can be modified using the Windows Form Designer.
- 'Do not modify it using the code editor.
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.mnuPaint = New System.Windows.Forms.MenuItem()
- Me.mnuResize = New System.Windows.Forms.MenuItem()
- Me.mnuPenAlignment = New System.Windows.Forms.MenuItem()
- Me.mnuPolygons = New System.Windows.Forms.MenuItem()
- Me.MainMenu1 = New System.Windows.Forms.MainMenu()
- Me.MenuItem1 = New System.Windows.Forms.MenuItem()
- Me.MenuItem9 = New System.Windows.Forms.MenuItem()
- Me.mnuLines = New System.Windows.Forms.MenuItem()
- Me.mnuCardinal = New System.Windows.Forms.MenuItem()
- Me.mnuBezier = New System.Windows.Forms.MenuItem()
- Me.mnuPens = New System.Windows.Forms.MenuItem()
- Me.mnuDashedLines = New System.Windows.Forms.MenuItem()
- Me.mnuPaths = New System.Windows.Forms.MenuItem()
- '
- 'mnuPaint
- '
- Me.mnuPaint.Index = 0
- Me.mnuPaint.Text = "Draw an ellipse in Paint event"
- '
- 'mnuResize
- '
- Me.mnuResize.Index = 1
- Me.mnuResize.Text = "Draw an ellipse in Resize event"
- '
- 'mnuPenAlignment
- '
- Me.mnuPenAlignment.Index = 8
- Me.mnuPenAlignment.Text = "Pen Alignments"
- '
- 'mnuPolygons
- '
- Me.mnuPolygons.Index = 4
- Me.mnuPolygons.Text = "Multiple Lines and Polygons"
- '
- 'MainMenu1
- '
- Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
- '
- 'MenuItem1
- '
- Me.MenuItem1.Index = 0
- Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuPaint, Me.mnuResize, Me.MenuItem9, Me.mnuLines, Me.mnuPolygons, Me.mnuCardinal, Me.mnuBezier, Me.mnuPens, Me.mnuPenAlignment, Me.mnuDashedLines, Me.mnuPaths})
- Me.MenuItem1.Text = "Examples"
- '
- 'MenuItem9
- '
- Me.MenuItem9.Index = 2
- Me.MenuItem9.Text = "-"
- '
- 'mnuLines
- '
- Me.mnuLines.Index = 3
- Me.mnuLines.Text = "Line, Rectangle, Ellipse"
- '
- 'mnuCardinal
- '
- Me.mnuCardinal.Index = 5
- Me.mnuCardinal.Text = "Cardinal splines"
- '
- 'mnuBezier
- '
- Me.mnuBezier.Index = 6
- Me.mnuBezier.Text = "Bezier splines"
- '
- 'mnuPens
- '
- Me.mnuPens.Index = 7
- Me.mnuPens.Text = "Pens"
- '
- 'mnuDashedLines
- '
- Me.mnuDashedLines.Index = 9
- Me.mnuDashedLines.Text = "Dashed lines and arrow caps"
- '
- 'mnuPaths
- '
- Me.mnuPaths.Index = 10
- Me.mnuPaths.Text = "Paths"
- '
- 'DrawingPrimitives
- '
- Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
- Me.ClientSize = New System.Drawing.Size(576, 341)
- Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.Menu = Me.MainMenu1
- Me.Name = "DrawingPrimitives"
- Me.Text = "Drawing primitives"
-
- End Sub
-
- #End Region
-
- Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
- If mnuPaint.Checked = False Then Exit Sub
-
- ' Get the Graphics object corresponding to the form's surface.
- Dim gr As Graphics = e.Graphics
- ' Clear the background.
- gr.Clear(Color.Azure)
- ' Draw an ellipse as large as the form.
- gr.DrawEllipse(Pens.Red, 0, 0, Me.ClientSize.Width, Me.ClientSize.Height)
- End Sub
-
-
- Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
- If mnuResize.Checked = False Then Exit Sub
-
- ' Get the Graphics object corresponding to the form's surface.
- Dim gr As Graphics = Me.CreateGraphics
- ' Clear the background.
- gr.Clear(Color.Azure)
- ' Draw an ellipse as large as the form.
- gr.DrawEllipse(Pens.Red, 0, 0, Me.ClientSize.Width, Me.ClientSize.Height)
- ' Destroy the Graphics object.
- gr.Dispose()
- End Sub
-
- Private Sub mnuPaint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuPaint.Click, mnuResize.Click
- With CType(sender, MenuItem)
- .Checked = Not .Checked
- End With
- ' Force a refresh.
- Me.Invalidate()
- End Sub
-
- Private Sub mnuLines_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuLines.Click
- ' Wait until the menu closes.
- Threading.Thread.Sleep(400)
-
- ' Create a Graphics object.
- Dim gr As Graphics = Me.CreateGraphics
- ' Draw a red line from (100, 30) to (500, 300).
- gr.DrawLine(Pens.Red, 100, 30, 500, 300)
- ' Draw a white square with left-top corner at (130, 50) and side=300 pixels.
- gr.DrawRectangle(Pens.White, New Rectangle(130, 50, 300, 300))
- ' Draw a green circle with radius=200 and center at (350,250)
- Dim r As Integer = 200
- gr.DrawEllipse(Pens.Green, 350 - r, 250 - r, r * 2, r * 2)
-
- Dim r2 As Integer = 100
- gr.DrawArc(Pens.Blue, 200 - r2, 150 - r2, r2 * 2, r2 * 2, 0, 90)
-
- ' Destroy the Graphics object.
- gr.Dispose()
- End Sub
-
- Private Sub mnuPolygons_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuPolygons.Click
- ' Wait until the menu closes.
- Threading.Thread.Sleep(400)
-
- ' Create a Graphics object.
- Dim gr As Graphics = Me.CreateGraphics
-
- ' Draw three straight lines.
- Dim points() As Point = {New Point(10, 10), New Point(100, 80), New Point(200, 20), New Point(300, 100)}
- gr.DrawLines(Pens.Black, points)
-
- ' Draw a rhomb.
- gr.DrawPolygon(Pens.Red, New Point() {New Point(200, 50), New Point(300, 100), New Point(200, 150), New Point(100, 100)})
-
- ' Draw three rectangles.
- Dim rects() As Rectangle = {New Rectangle(50, 30, 200, 100), New Rectangle(70, 40, 220, 110), New Rectangle(90, 50, 240, 120)}
- gr.DrawRectangles(Pens.Green, rects)
-
- ' Destroy the Graphics object.
- gr.Dispose()
- End Sub
-
- Private Sub mnuCardinal_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuCardinal.Click
- ' Wait until the menu closes.
- Threading.Thread.Sleep(400)
-
- ' Create a Graphics object.
- Dim gr As Graphics = Me.CreateGraphics
- gr.Clear(Color.White)
-
- Dim points() As Point = {New Point(100, 100), New Point(200, 200), New Point(250, 30), New Point(350, 100)}
- Dim tension As Single
-
- For tension = 0 To 2 Step 0.5
- gr.DrawCurve(Pens.Black, points, tension)
- Next
-
- ' Destroy the Graphics object.
- gr.Dispose()
- End Sub
-
- Private Sub mnuBezier_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuBezier.Click
- ' Wait until the menu closes.
- Threading.Thread.Sleep(400)
-
- ' Create a Graphics object.
- Dim gr As Graphics = Me.CreateGraphics
- gr.Clear(Color.White)
-
- gr.DrawBezier(Pens.Black, New Point(10, 30), New Point(100, 20), New Point(140, 190), New Point(200, 200))
- gr.DrawLine(Pens.Red, New Point(10, 30), New Point(100, 20))
- gr.DrawLine(Pens.Red, New Point(200, 200), New Point(140, 190))
-
-
- ' Destroy the Graphics object.
- gr.Dispose()
- End Sub
-
- Private Sub mnuPens_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuPens.Click
- ' Wait until the menu closes.
- Threading.Thread.Sleep(400)
-
- ' Create a Graphics object.
- Dim gr As Graphics = Me.CreateGraphics
- gr.Clear(Color.White)
-
- ' Draw the inner rectanble with a pen of custom color.
- Dim p1 As New Pen(Color.FromArgb(128, 0, 60))
- gr.DrawRectangle(p1, 50, 50, 100, 50)
- ' Draw the outer rectangle with a blue pen 4 pixel wide.
- Dim p2 As New Pen(Color.Blue, 4)
- gr.DrawRectangle(p2, 10, 10, 200, 100)
-
- ' Destroy the Pen objects
- p1.Dispose()
- p2.Dispose()
-
- ' Destroy the Graphics object.
- gr.Dispose()
- End Sub
-
- Private Sub mnuPenAlignment_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuPenAlignment.Click
- ' Wait until the menu closes.
- Threading.Thread.Sleep(400)
-
- ' Create a Graphics object.
- Dim gr As Graphics = Me.CreateGraphics
- gr.Clear(Color.White)
-
- Dim p As New Pen(Color.Yellow, 12)
- Dim rect As New Rectangle(20, 20, 100, 100)
-
- gr.DrawRectangle(p, rect)
- gr.DrawRectangle(Pens.Black, rect)
-
- p.Alignment = Drawing.Drawing2D.PenAlignment.Inset
- rect.Offset(150, 0)
- gr.DrawRectangle(p, rect)
- gr.DrawRectangle(Pens.Black, rect)
- p.Dispose()
-
- ' Enable this block of code to show that other alignment settings have no effect
- #If 0 Then
- p = New Pen(Color.Red, 12)
- p.Alignment = Drawing.Drawing2D.PenAlignment.Left
- gr.DrawLine(p, 20, 180, 200, 180)
- gr.DrawLine(Pens.Black, 20, 180, 200, 180)
- p.Dispose()
-
-
- p = New Pen(Color.Yellow, 12)
- p.Alignment = Drawing.Drawing2D.PenAlignment.Right
- gr.DrawLine(p, 20, 220, 200, 220)
- gr.DrawLine(Pens.Black, 20, 220, 200, 220)
- p.Dispose()
- #End If
-
- ' Destroy the Graphics object.
- gr.Dispose()
- End Sub
-
- Private Sub mnuDashedLines_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuDashedLines.Click
- ' Wait until the menu closes.
- Threading.Thread.Sleep(400)
-
- ' Create a Graphics object.
- Dim gr As Graphics = Me.CreateGraphics
- gr.Clear(Color.White)
-
- ' a custom pen
- Dim p As New Pen(Color.Black, 3)
- p.DashStyle = Drawing.Drawing2D.DashStyle.Dash
- gr.DrawLine(p, 10, 10, 200, 10)
- p.DashStyle = Drawing.Drawing2D.DashStyle.DashDot
- gr.DrawLine(p, 10, 30, 200, 30)
- p.DashStyle = Drawing.Drawing2D.DashStyle.DashDotDot
- gr.DrawLine(p, 10, 50, 200, 50)
- p.DashStyle = Drawing.Drawing2D.DashStyle.Dot
- gr.DrawLine(p, 10, 70, 200, 70)
-
- ' create a custom dash pattern.
- Dim sngArray() As Single = {4, 4, 8, 4, 12, 4}
- p.DashPattern = sngArray
- gr.DrawLine(p, 10, 90, 200, 90)
-
- ' display pens with cap
- Dim p2 As New Pen(Color.Black, 8)
- p2.StartCap = Drawing.Drawing2D.LineCap.DiamondAnchor
- p2.EndCap = Drawing.Drawing2D.LineCap.ArrowAnchor
- gr.DrawLine(p2, 280, 30, 500, 30)
-
- p2.StartCap = Drawing.Drawing2D.LineCap.RoundAnchor
- p2.EndCap = Drawing.Drawing2D.LineCap.Round
- gr.DrawLine(p2, 280, 70, 500, 70)
-
-
- ' Destroy the Pen object.
- p.Dispose()
- p2.Dispose()
- ' Destroy the Graphics object.
- gr.Dispose()
-
- End Sub
-
- Private Sub mnuPaths_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuPaths.Click
- ' Wait until the menu closes.
- Threading.Thread.Sleep(400)
-
- ' Create a Graphics object.
- Dim gr As Graphics = Me.CreateGraphics
- gr.Clear(Color.White)
-
- ' Create a new Path object.
- Dim pa As New GraphicsPath()
- ' Start a figure.
- pa.StartFigure()
- pa.AddRectangle(New Rectangle(20, 20, 200, 150))
- pa.AddRectangle(New Rectangle(50, 50, 200, 150))
- pa.AddLine(20, 20, 50, 50)
- pa.StartFigure() ' Avoid connecting the two segments.
- pa.AddLine(220, 20, 250, 50)
- pa.StartFigure() ' Avoid connecting the two segments.
- pa.AddLine(220, 170, 250, 200)
- pa.StartFigure() ' Avoid connecting the two segments.
- pa.AddLine(20, 170, 50, 200)
-
- ' Start another (closed) figure.
- pa.StartFigure()
- pa.AddLine(300, 20, 400, 20)
- pa.AddLine(400, 20, 400, 120)
- pa.CloseFigure()
-
- ' Draw the path, using a thick red pen
- Dim p As New Pen(Color.Red, 3)
- gr.DrawPath(p, pa)
-
- pa.Dispose()
- p.Dispose()
-
-
- ' Destroy the Graphics object.
- gr.Dispose()
- End Sub
- End Class
-